import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class Game {
	/**
	 * Main Class
	 * Game is the encapsulation of Board, Ship, and Square.  These Objects are required for game play.
	 * Game gets user input from the user and builds the board and the ship.
	 * 
	 * @author Larry Savago
	 * @author Casey Huckins
	 * @date 4/13/2012
	 *
	 */

	private boolean firstTime = false;
	private static boolean run = true;
	private Ball ball = new Ball();
	private Board board = new Board();
	FileWrite file = new FileWrite();
	BruteForce bSearch = new BruteForce();
	Reinforcement rSearch = new Reinforcement();
	
	int count;
	
	private void welcome() {
		// called by play()
		// only called once
		
		System.out.println("\nWelcome to AI Battleship\n");
		System.out.println("Programmed by Casey and Larry\n");
		System.out.println("This is a Proof of Concept program. It is not\ndesigned to have error checking on user input");
		System.out.println("Train system? Press 1 to train, 2 to play!");
		Scanner input=new Scanner(System.in);
		String s;
		s = input.nextLine();
		try {
			if (s.contains("1")) {
				rSearch.setTrained(false);
				System.out.println("Training..");
			}
			else {
				rSearch.setTrained(true);
				System.out.println("Playing...");
			}
		}
		catch (Exception e) {
			System.out.println("Opps! We had an error!\n");
			return;			
		}
		
		System.out.println("Pressing 'q' at a prompt will exit!");
		firstTime = true;
		file.clear();
		count = 1;
	}
	
	private int getCount() {
		return count;
	}
	
	private void addCount() {
		count++;
	}
	
	
	private void getBallPosition() {
		// called by play()
		// get top position of ship
		Scanner input=new Scanner(System.in);
		String s;
		System.out.println(board.toString());
		System.out.println("Enter the coordinates that corresponds to the Top/Left Postion of the ball");
		System.out.println("Example, enter 1,1 for top left corner of board\n");
		
		s = input.nextLine();
		String[] t;
		try {
			t = s.split(",");
			System.out.println("Your Ball is in Row " + t[0] + " and Col " + t[1]);
			
			// build ball ** subtract 1 off for zero index arrays **
			setBall(Integer.parseInt(t[0])-1, Integer.parseInt(t[1])-1);	
		}
		catch (Exception e) {
			if (s.equals("q")||s.equals("Q")) {
				System.out.println("Shutting Down!");
				stop();
				return;
			}
			System.out.println("Opps! We had an error, starting over!\n");
			return;			
		}
	}
	
	private void play() {
		
		// method that calls helper methods in class Game
		if (!firstTime) {
			welcome();
		}
		
		
		// check for exit
		if (run) {
			getBallPosition();
		}
		
		// check for exit
		if (run) {
			// print out ball
			System.out.println(ball.toString());
			file.write("......................\r\nBall " + count + "\r\n" + ball.toString() + "\r\n");
			// brute force search
			file.write("Brute Force Search - count = " + bSearch.search(board, ball) + "\r\n");
			file.write("Reinforcement Search - count = " + rSearch.search(board, ball) + "\r\n\r\n");
			
		}
		
		
	}
	
	private void setBall(int row, int col) {
		ball.set(board.getSquare(row, col));
	}

	
	private void stop() {
		run = false;
		file.open();
		if (!rSearch.isTrained()) {
			rSearch.learn();
		}

	}
	
	
	// main method that runs when program is called
	public static void main(String[] args) {
		
		Game g = new Game();

		while(run) {
			System.out.println("\nGame " + g.getCount());
			g.play();
			g.addCount();
		}
		
	}

		//code to write to a file
		class FileWrite 
		 {
			  public void write(String text)
			   {
			   try{
				   // Create file 
				   //FileWriter fstream = new FileWriter("battleship.txt");
				   // Append to a file
				   FileWriter fstream = new FileWriter("battleship.txt",true);
				   
				   BufferedWriter out = new BufferedWriter(fstream);  
				   out.write(text);
				   //Close the output stream
				   out.close();
			   }catch (Exception e){//Catch exception if any
				   System.err.println("Error: " + e.getMessage());
			   }
			 }// write()
			  
			  public void clear() {
				  try {
					FileWriter fstream = new FileWriter("battleship.txt");
					BufferedWriter out = new BufferedWriter(fstream);  
					out.write("");
				} catch (IOException e) {
					e.printStackTrace();
				}
				  
			  }
			  public void open() {
				  // open in notepad
				 
		        try {
					Runtime.getRuntime().exec("notepad battleship.txt");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				  
			  }
			 
			
	}// end file write
}// end Game 
